package com.toonly.defaultvalues.framework;

import java.util.ArrayList;
import java.util.List;

public abstract class AChainedDefaultValueGenerator<T> implements IDefaultValueGenerator<T>, IDefaultValueGeneratorChain<T> {

    protected T source;
    private List<IDefaultValueGenerator<T>> chain = new ArrayList<>();

    @Override
    public void set(T source) {
        this.source = source;
    }

    @Override
    public IGeneratedValue generate() {
        for (IDefaultValueGenerator<T> defaultValueGenerator : chain) {
            defaultValueGenerator.set(this.source);
            IGeneratedValue generatedValue = defaultValueGenerator.generate();
            if (generatedValue.suc()) {
                return new GeneratedValue(true, generatedValue.val());
            }
        }
        return new GeneratedValue(false, null);
    }

    @Override
    public void add(IDefaultValueGenerator<T> generator) {
        chain.add(generator);
    }

}
